package com.qianfeng.test;

import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.AdapterView;

import com.alibaba.fastjson.JSONObject;
import com.handmark.pulltorefresh.library.PullToRefreshBase;
import com.handmark.pulltorefresh.library.PullToRefreshListView;
import com.loopj.android.http.AsyncHttpClient;
import com.loopj.android.http.RequestParams;
import com.loopj.android.http.TextHttpResponseHandler;
import com.qianfeng.test.adapter.GridViewItemAdapter;
import com.qianfeng.test.bean.gridview_item_bean.DataEntity;
import com.qianfeng.test.bean.gridview_item_bean.Gridview_item_bean;
import com.qianfeng.test.constants.UrlConstans;

import org.apache.http.Header;

import java.util.ArrayList;
import java.util.List;

public class GridViewActivity extends AppCompatActivity implements AdapterView.OnItemClickListener,PullToRefreshBase.OnRefreshListener2 {
    private boolean isRefresh = false;
    private int page = 1;
    private PullToRefreshListView ptrListView;
    private List<DataEntity> list = new ArrayList<>();
    private GridViewItemAdapter gridAdapter;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_grid_view);
        initData();
    }

    private void initData() {
        ptrListView = (PullToRefreshListView) findViewById(R.id.gridView_ptrlistview);
        gridAdapter = new GridViewItemAdapter(list,this);

        ptrListView.setAdapter(gridAdapter);
        //设置为自动刷新
        ptrListView.setRefreshing();
        ptrListView.setOnItemClickListener(this);
        ptrListView.setOnRefreshListener(this);
        Intent intent = this.getIntent();
        String type = intent.getStringExtra("type");
        AsyncHttpClient client = new AsyncHttpClient();
        RequestParams params = new RequestParams();
        params.put("page",page);
        params.put("type",type);
        client.get(UrlConstans.Url_GridView, params, new TextHttpResponseHandler() {
            @Override
            public void onFailure(int statusCode, Header[] headers, String responseString, Throwable throwable) {
                ptrListView.onRefreshComplete();
            }

            @Override
            public void onSuccess(int statusCode, Header[] headers, String responseString) {
                if(!TextUtils.isEmpty(responseString)){
                    Gridview_item_bean bean = JSONObject.parseObject(responseString,Gridview_item_bean.class);
                    if(bean!=null&&bean.getResult()!=null){

                        if(bean.getResult().getData().size()!=0){
                            if(page == 1&&list.size()>0){
                                list.clear();
                            }
                        }
                        list.addAll(bean.getResult().getData());
                        gridAdapter.notifyDataSetChanged();
                        ptrListView.onRefreshComplete();
                    }
                }
            }
        });
    }


    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {

    }

    @Override
    public void onPullDownToRefresh(PullToRefreshBase refreshView) {

        initData();
        isRefresh = true;
    }

    @Override
    public void onPullUpToRefresh(PullToRefreshBase refreshView) {
        page++;
        initData();

    }
}
